home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / basic / _partition.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  1.4 KB  |  77 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _partition.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. //------------------------------------------------------------------------------
  17. //
  18. // partitions
  19. //
  20. // implementation: union find with weighted union rule and path compression
  21. //
  22. // S. N.  (1989)
  23. //------------------------------------------------------------------------------
  24.  
  25. #include <LEDA/partition.h>
  26.  
  27.  
  28. void partition::clear()
  29. { // free all used items
  30.   partition_item p = used_items; 
  31.   while (used_items)
  32.   { p = used_items;
  33.     used_items = used_items->next;
  34.     clear_inf(p->info);
  35.     delete p;
  36.    }
  37.  }
  38.   
  39.  
  40.  
  41. partition_item partition::find(partition_item y) 
  42. { // find with path compression
  43.  
  44.   register partition_item x = y->father;
  45.  
  46.   if (x==0) return y;
  47.  
  48.   register partition_item root = y;
  49.  
  50.   while (root->father) root = root->father;
  51.  
  52.   while (x!=root)
  53.   { y->father = root;
  54.     y = x;
  55.     x = y->father;
  56.    }
  57.   return root;
  58.  }
  59.  
  60. void partition::union_blocks(partition_item a, partition_item b)
  61. { // weighted union
  62.  
  63.   a = find(a);
  64.   b = find(b);
  65.  
  66.   if (a==b) return;
  67.  
  68.   if (a->size > b->size)
  69.        { b->father = a;
  70.          a->size += b->size; }
  71.   else { a->father = b;
  72.          b->size += a->size; }
  73.  
  74.  }
  75.  
  76.